package cc.rengu.igas.cops.common.dao.impl;

import cc.rengu.igas.cops.common.dao.CopsUserHisOrderInfoMapper;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.entity.UserOrderInfo;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.dbs.Database;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

/**
 * @Description 用户订单表操作实现
 * @Author panky
 * @date 2020/5/18
 **/
public class CopsUserHisOrderInfoMapperImpl implements CopsUserHisOrderInfoMapper {
    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    private String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppParamConstant.DATABASE_CONF_NODE, AppParamConstant.OLTP_POOL_NAME);

    @Override
    public UserOrderInfo selectUserOrderInfoByInstIdMsgSrcIdChannelIdTraceNo(String instId, String msgSrcId, String channelId, String traceNo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppParamConstant.OLTP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "copsSelecHisUserOrderInfoByInstIdMsgSrcIdChannelIdTraceNo");

        UserOrderInfo copsUserOrderInfo = new UserOrderInfo();
        copsUserOrderInfo.setInstId(instId);
        copsUserOrderInfo.setFrontSeqNum(traceNo);
        copsUserOrderInfo.setMsgSrcId(msgSrcId);
        copsUserOrderInfo.setTransChannelId(channelId);

        int recode = dbsUtil.dbsExecuteSql(sqlStr, copsUserOrderInfo, copsUserOrderInfo);
        if (Database.DBS_NOT_FOUND == recode) {
            rglog.info("用户订单信息查询无记录，instId:<{}>,msgSrcId:<{}>,channelId:<{}>,frontSeqNum:<{}>", instId, msgSrcId, channelId, traceNo);
            return null;
        } else if (Database.DBS_SUCCESS != recode) {
            rglog.info("用户订单信息查询失败，instId:<{}>,msgSrcId:<{}>,channelId:<{}>,frontSeqNum:<{}>", instId, msgSrcId, channelId, traceNo);
            return null;
        }
        return copsUserOrderInfo;
    }

    @Override
    public UserOrderInfo selectUserOrderInfoByInstIdSysSeqNo(String instId, String sysSeqNo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppParamConstant.OLTP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "copsSelectUserHisOrderInfoBySecondKey");

        UserOrderInfo copsUserOrderInfo = new UserOrderInfo();
        copsUserOrderInfo.setInstId(instId);
        copsUserOrderInfo.setSysSeqNum(sysSeqNo);

        int recode = dbsUtil.dbsExecuteSql(sqlStr, copsUserOrderInfo, UserOrderInfo.class);
        if (Database.DBS_NOT_FOUND == recode) {
            rglog.info("用户订单历史表中不存在该条数据，instId:<{}>,sysSeqNum:<{}>", instId, sysSeqNo);
            return null;
        } else if (Database.DBS_SUCCESS != recode) {
            rglog.info("用户订单历史表查询失败，instId:<{}>,sysSeqNum:<{}>", instId, sysSeqNo);
            return null;
        }

        return copsUserOrderInfo;
    }

    @Override
    public UserOrderInfo selectUserOrderInfoBySecondKey(String instId, String mchntOrderId, String msgSrcId, String transChannelId) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppParamConstant.OLTP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectUserHisOrderInfoByMchntOrderId");
        UserOrderInfo userOrderInfo = new UserOrderInfo();
        userOrderInfo.setInstId(instId);
        userOrderInfo.setMchntOrderId(mchntOrderId);
        userOrderInfo.setMsgSrcId(msgSrcId);
        userOrderInfo.setTransChannelId(transChannelId);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, userOrderInfo, userOrderInfo);
        if (0 != iReturnCode) {
            rglog.error("根据商户订单号查询用户订单信息失败,instId:<{}>,mchntOrderId:<{}>,msgSrcId:<{}>,transChannelId:<{}>,iReturnCode:<{}>", instId, mchntOrderId, msgSrcId, transChannelId, iReturnCode);
            return null;
        }
        return userOrderInfo;
    }
}
